package com.meicet.adapter.view

import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.annotation.Keep
import com.meicet.adapter.R
import com.meicet.adapter.utils.gone
@Keep
class StatusView(context: Context, attr: AttributeSet?) :
    FrameLayout(context, attr),
    StatusListener {
    companion object {
        private const val statusEmpty = 1
        private const val statusLoading = 2
        private const val statusError = 3
    }

    constructor(context: Context) : this(context, null)

    private var currentState = statusEmpty
    var onCallback = object : StatusListener.OnCallBackStatus {
        override fun onClickError(statusEmpty: Boolean) {
            //
        }
    }

    override fun setCallBackStatus(callBack: StatusListener.OnCallBackStatus) {
        this.onCallback = callBack
        if(childCount==2){
            if(getChildAt(0)!=statusRootView){
                childContent=getChildAt(0)
                Log.i("StatusView","childContent=0")
            }else if(getChildAt(1)!=statusRootView){
                childContent=getChildAt(1)
                Log.i("StatusView","childContent=1")
            }
        }
    }

    override fun getStatusView() = this

    val emptyInfo by lazy { findViewById<TextView>(R.id.emptyInfo) }
    val errorInfo by lazy { findViewById<TextView>(R.id.errorInfo) }
    val loadInfo by lazy { findViewById<TextView>(R.id.loadInfo) }

    override fun setTextColor(color: Int) {
        emptyInfo.setTextColor(color)
        errorInfo.setTextColor(color)
        loadInfo.setTextColor(color)
    }

    override fun setTextSize(textSP: Float) {
        emptyInfo.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSP)
        errorInfo.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSP)
        loadInfo.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSP)
    }

    fun setEmptyText(info: String) {
        emptyInfo.text = info
    }

    fun setErrorText(info: String) {
        errorInfo.text = info
    }

    fun setLoadText(info: String) {
        loadInfo.text = info
    }

    override fun showEmpty(info: String) {
        currentState = statusEmpty
        if (info.isNotEmpty()) {
            emptyInfo.text = info
        }
        refreshView()
    }

    override fun showLoading(info: String) {
        currentState = statusLoading
        if (info.isNotEmpty()) {
            loadInfo.text = info
        }
        refreshView()
    }

    override fun showError(info: String) {
        currentState = statusError
        if (info.isNotEmpty()) {
            errorInfo.text = info
        }
        refreshView()
    }

    var showContentState = false
    override fun showContent() {
        statusRootView.gone(true)
        childContent?.gone(false)
        showContentState = true
    }

    val emptyView by lazy { findViewById<View>(R.id.emptyView) }
    val loadView by lazy { findViewById<View>(R.id.loadView) }
    val errorView by lazy { findViewById<View>(R.id.errorView) }
    val statusRootView by lazy { findViewById<View>(R.id.statusRootView) }
    //子布局 当只有一个子view时
    var childContent:View?=null
    private fun refreshView() {
        emptyView.gone(currentState != statusEmpty)
        loadView.gone(currentState != statusLoading)
        errorView.gone(currentState != statusError)
        if (showContentState) {
            statusRootView.gone(false)
            childContent?.gone(false)
            showContentState = false
        }else{
            childContent?.gone(true)
        }
    }

    init {
        View.inflate(context, R.layout._status_view, this)
        statusRootView.setOnClickListener {
            if (currentState == statusError || currentState == statusEmpty) {
                onCallback.onClickError(currentState == statusEmpty)
            }
        }
    }
}
